package com.tramsun.libs.prefcompat;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class Logger {
  private static final String TAG = Pref.class.getSimpleName();
  private static int logLevel = HiLog.ERROR;
  static Logger sLogger;
  private static final int DOMAIN_ID = 0xD000F00;
  private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, Logger.TAG);
  private static final String LOG_FORMAT = "%{public}s: %{public}s";

  public static void setLogLevel(int logLevel) {
    if (logLevel >= HiLog.LOG_APP && logLevel <= HiLog.FATAL) {
      Logger.logLevel = logLevel;
    } else {
      HiLog.error(LABEL_LOG, LOG_FORMAT, TAG, "Invalid log level. Defaulting to Log.ERROR");
      Logger.logLevel = HiLog.ERROR;
    }
  }

  public static Logger getLogger() {
    if (sLogger == null) {
      sLogger = new Logger();
    }
    return sLogger;
  }

  public void v(String msg) {
    if (logLevel <= HiLog.LOG_APP) {
      HiLog.debug(LABEL_LOG, LOG_FORMAT, TAG, msg);
    }
  }

  public void i(String msg) {
    if (logLevel <= HiLog.INFO) {
      HiLog.debug(LABEL_LOG, LOG_FORMAT, TAG, msg);
    }
  }

  public void d(String msg) {
    if (logLevel <= HiLog.FATAL) {
      HiLog.debug(LABEL_LOG, LOG_FORMAT, TAG, msg);
    }
  }

  public void w(String msg) {
    if (logLevel <= HiLog.INFO) {
      HiLog.debug(LABEL_LOG, LOG_FORMAT, TAG, msg);
    }
  }

  public void e(String msg) {
    if (logLevel <= HiLog.ERROR) {
      HiLog.debug(LABEL_LOG, LOG_FORMAT, TAG, msg);
    }
  }

  public void a(String msg) {
    if (logLevel <= HiLog.INFO) {
      HiLog.debug(LABEL_LOG, LOG_FORMAT, TAG, msg);
    }
  }
}
